package com.lw.io.properties;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import java.util.Set;
import java.util.function.Consumer;

public class PropertiesTest {
    public static void main(String[] args) throws IOException {
       //Properties的步骤
        //1.创建Properties集合对象，使用Properties集合对象中的方法load，把配置文件中的数据，读取到集合中
        Properties properties = new Properties();
        properties.load(PropertiesTest.class.getClassLoader().getResourceAsStream("test.properties"));
        properties.setProperty("name","刘伟");
        //2.使用Properties集合中的方法getProperty，通过键获取值
           String username = properties.getProperty("username");
           System.out.println("username = " + username);
        //3.遍历Properties集合
        Set<Object> set = properties.keySet();
        set.forEach(new Consumer<Object>() {
            @Override
            public void accept(Object key) {
                System.out.println(key + "=" + properties.getProperty(key.toString()));
            }
        });
        properties.store(new OutputStreamWriter( new FileOutputStream("test.properties"),"UTF-8"),"test");


    }

}
